import { onActivated, ref } from "vue"
import { onBeforeRouteLeave } from "vue-router"
import { determineNeedSaveKeepALive } from "@/utils/routerKeepALive.js"

export const useKeepalive = (needSaveRoute, componentName) => {
  const scrollYEle = ref(null)
  let scrollTop = 0
  onActivated(() => {
    const scrollElement = scrollYEle.value
    scrollElement?.scrollTo({ top: scrollTop, behavior: 'instant' })
  })
  onBeforeRouteLeave((to, from) => {
    scrollTop = scrollYEle.value?.scrollTop
    determineNeedSaveKeepALive(to, from, needSaveRoute, componentName)
  })
  return { scrollYEle }
}